import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import userModel from '@/model/user'
const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref()
    const token = ref('')
    const isLogin = computed(() => {
      return !!token.value
    })
    const login = async (code: string, state: string) => {
      const data = await userModel.login({ code, state })
      console.log(data)
      userInfo.value = data.profile
      token.value = data.token
    }

    const logout = () => {
      userInfo.value = null
      token.value = ''
    }

    return {
      logout,
      isLogin,
      userInfo,
      token,
      login
    }
  },
  {
    persist: {
      storage: localStorage
    }
  }
)

export default useUserStore
